﻿using System;
using System.Collections.Generic;
using YhmOA.Pager;
using YhmOA.Common;
using YhmOA.IDAL;
using System.Web.UI.WebControls;

namespace YhmOA
{
    public class ManagerPage<T> : BasePage where T : class
    {
        public ITDal<T> Dal
        {
            get { return _dal ?? DataProvider.GetGEFDal<ITDal<T>>(); }
        }
        private ITDal<T> _dal;

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            YhmOA.Pager.Pager pager = this.Page.FindControl("gvShowPager") as YhmOA.Pager.Pager;
            pager.GetDataMethoder = GetPagerDataSources;
            pager.BindDataSources += BindDataSources;
        }

        private void BindDataSources(object sender, PagerEventArgs e)
        {
            GridView gvShow = this.Page.FindControl("gvShow") as GridView;
            gvShow.DataSource = e.DataSources;
            gvShow.AutoGenerateColumns = false;
            gvShow.DataBind();
        }

        #region 页面排序事件
        /// <summary>
        /// 页面排序事件
        /// </summary>
        /// <param name="orderName"></param>
        [AjaxMethod]
        public virtual void Sort(string orderName)
        {
            this.SetAjaxRenderControls(this.Page.FindControl("gvShow"));
            YhmOA.Pager.Pager pager = this.Page.FindControl("gvShowPager") as YhmOA.Pager.Pager;
            pager.BindDataSources += new Pager.Pager.BindDataSourcesHandler(BindDataSources);
            pager.GetDataMethoder = GetPagerDataSources;
            pager.OrderName = orderName;
            pager.Rebind();
            
        }
        #endregion

        #region 页面搜索事件
        /// <summary>
        /// 页面搜索事件
        /// </summary>
        [AjaxMethod]
        public virtual void Search()
        {
            this.SetAjaxRenderControls(this.Page.FindControl("gvShow"));
            YhmOA.Pager.Pager pager = this.Page.FindControl("gvShowPager") as YhmOA.Pager.Pager;
            pager.BindDataSources += BindDataSources;
            pager.GetDataMethoder = GetPagerDataSources;
            pager.Rebind();
        }
        #endregion

        #region 为分页控件获取数据源
        /// <summary>
        /// 为分页控件获取数据源
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">当前页大小</param>
        /// <returns></returns>
        public virtual Data GetPagerDataSources(int pageIndex, int pageSize)
        {
            this.SetAjaxRenderControls(this.Page.FindControl("gvShow"));
            int recCount = 0;
            List<T> userList = new List<T>();
            YhmOA.Pager.Pager pager = this.Page.FindControl("gvShowPager") as YhmOA.Pager.Pager;
            string searchText = string.Empty;
            try
            {
                searchText = (this.Page.FindControl("txt_Search") as TextBox).Text;
            }
            catch
            {
                searchText = string.Empty;
            }
            string whereText = string.Empty;
            try
            {
                whereText = (ViewState["WhereStr"] ?? "").ToString();
            }
            catch
            {
                whereText = string.Empty;
            }
            
            if (string.Empty.Equals(searchText))
            {
                userList = Dal.GetList(whereText, pageSize, pageIndex, pager.OrderName + pager.SortOrder, out recCount);
            }
            else
            {
                userList = Dal.SearchTable(whereText, "*", searchText, pageIndex, pageSize, pager.OrderName, pager.SortOrder, out recCount);
            }
            return new Data(recCount, userList);
        }
        #endregion

        #region 删除操作
        /// <summary>
        /// 删除操作
        /// </summary>
        /// <param name="listID">ID列表(1_2_3)</param>
        [AjaxMethod]
        public void Delete(string listID)
        {
            //删除代码
            Dal.DeleteByIDS(listID);
            //删除成功刷新页面
            Search();
        }
        #endregion

        #region 删除选中的列
        /// <summary>
        /// 删除选中的列
        /// </summary>
        [AjaxMethod]
        public void DeleteData()
        {
            this.SetNotAjaxRenderControls(this.Page.FindControl("gvShow"));
            string ItemChk = Request.Form["ItemChk"] ?? "";
            if (ItemChk == "")
            {
                MessageBox("请选择项");
            }
            else
            {
                this.Confirm("确定要删除吗", String.Format("Delete('{0}');", ItemChk));
            }
        }
        #endregion

        #region 权限相关
        /// <summary>
        /// 关于服务器控件的服务端事件的权限
        /// </summary>
        /// <param name="FormMode">权限的类型</param>
        /// <param name="ctrlID">服务器控件ID</param>
        [AjaxMethod]
        public void RoleVerificationCtrl(string FormMode, string ctrlID)
        {
            bool flag = true;
            //flag = false;
            if (flag)
            {
                if (FormMode == "3")
                {
                    SetNotAjaxRenderControls(this.Page.FindControl("gvShow"));
                }
                ExecClientScript("__doPostBack('" + ctrlID + "', '');");
            }
            else
            {
                MessageBox("您没有权限");
            }
        }
        /// <summary>
        /// 关于客户端控件的权限
        /// </summary>
        /// <param name="FormMode">权限的类型</param>
        /// <param name="script">有权限时执行的代码</param>
        [AjaxMethod]
        public void RoleVerification(string FormMode,string script)
        {
            bool flag = true;
            //flag = false;
            if (flag)
            {
                if (FormMode == "3")
                {
                    SetNotAjaxRenderControls(this.Page.FindControl("gvShow"));
                }
                this.ExecClientScript(script);
            }
            else
            {
                MessageBox("您没有权限");
            }
        }
        #endregion
    }
}
